// 购物车服务类
class CartService {
    constructor() {
      // 从本地存储获取购物车数据，如果没有则初始化为空数组
      this.cart = JSON.parse(localStorage.getItem('bookstore-cart')) || [];
    }
    
    // 保存购物车到本地存储
    saveCart() {
      localStorage.setItem('bookstore-cart', JSON.stringify(this.cart));
    }
    
    // 获取购物车所有商品
    getCartItems() {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(this.cart);
        }, 200);
      });
    }
    
    // 添加商品到购物车
    addToCart(bookId, quantity = 1) {
      return new Promise((resolve) => {
        setTimeout(() => {
          // 检查商品是否已在购物车中
          const existingItemIndex = this.cart.findIndex(item => item.bookId === bookId);
          
          if (existingItemIndex !== -1) {
            // 如果已存在，增加数量
            this.cart[existingItemIndex].quantity += quantity;
          } else {
            // 如果不存在，添加新商品
            this.cart.push({
              bookId,
              quantity
            });
          }
          
          // 保存购物车
          this.saveCart();
          resolve(this.cart);
        }, 300);
      });
    }
    
    // 更新购物车商品数量
    updateCartItem(bookId, quantity) {
      return new Promise((resolve) => {
        setTimeout(() => {
          // 查找商品
          const itemIndex = this.cart.findIndex(item => item.bookId === bookId);
          
          if (itemIndex !== -1) {
            if (quantity <= 0) {
              // 如果数量为0，移除商品
              this.cart.splice(itemIndex, 1);
            } else {
              // 更新数量
              this.cart[itemIndex].quantity = quantity;
            }
            
            // 保存购物车
            this.saveCart();
          }
          
          resolve(this.cart);
        }, 200);
      });
    }
    
    // 从购物车移除商品
    removeFromCart(bookId) {
      return new Promise((resolve) => {
        setTimeout(() => {
          // 过滤掉要移除的商品
          this.cart = this.cart.filter(item => item.bookId !== bookId);
          
          // 保存购物车
          this.saveCart();
          resolve(this.cart);
        }, 200);
      });
    }
    
    // 清空购物车
    clearCart() {
      return new Promise((resolve) => {
        setTimeout(() => {
          this.cart = [];
          this.saveCart();
          resolve(this.cart);
        }, 200);
      });
    }
    
    // 获取购物车商品总数
    getTotalItems() {
      return new Promise((resolve) => {
        setTimeout(() => {
          const total = this.cart.reduce((sum, item) => sum + item.quantity, 0);
          resolve(total);
        }, 100);
      });
    }
    
    // 获取购物车总金额
    getTotalAmount(books) {
      return new Promise((resolve) => {
        setTimeout(() => {
          let total = 0;
          
          this.cart.forEach(item => {
            const book = books.find(b => b.id === item.bookId);
            if (book) {
              const price = book.discount ? book.price * book.discount : book.price;
              total += price * item.quantity;
            }
          });
          
          resolve(total);
        }, 100);
      });
    }
  }
  
  export default new CartService();
  